package com.yami.trading.huobi.data.websocket.service.huobi;

import com.alibaba.fastjson.JSONObject;
import com.yami.trading.huobi.data.websocket.client.MarketClient;
import com.yami.trading.huobi.data.websocket.client.req.market.SubMarketDepthRequest;
import com.yami.trading.huobi.data.websocket.client.req.market.SubMarketTickerRequest;
import com.yami.trading.huobi.data.websocket.client.req.market.SubMarketTradeRequest;
import com.yami.trading.huobi.data.websocket.constant.Options;
import com.yami.trading.huobi.data.websocket.constant.enums.DepthStepEnum;
import com.yami.trading.huobi.data.websocket.model.market.MarketDepthEvent;
import com.yami.trading.huobi.data.websocket.model.market.MarketTickerEvent;
import com.yami.trading.huobi.data.websocket.model.market.MarketTradeEvent;
import com.yami.trading.huobi.data.websocket.service.huobi.connection.HuobiRestConnection;
import com.yami.trading.huobi.data.websocket.service.huobi.connection.HuobiWebSocketConnection;
import com.yami.trading.huobi.data.websocket.service.huobi.parser.market.MarketDepthEventParser;
import com.yami.trading.huobi.data.websocket.service.huobi.parser.market.MarketTickerEventParser;
import com.yami.trading.huobi.data.websocket.service.huobi.parser.market.MarketTradeEventParser;
import com.yami.trading.huobi.data.websocket.utils.InputChecker;
import com.yami.trading.huobi.data.websocket.utils.ResponseCallback;
import com.yami.trading.huobi.data.websocket.utils.SymbolUtils;


import java.util.ArrayList;
import java.util.List;

public class HuobiMarketService implements MarketClient {

	private Options options;

	private HuobiRestConnection restConnection;

	public HuobiMarketService(Options options) {
		this.options = options;
		restConnection = new HuobiRestConnection(options);
	}

	public static final String WEBSOCKET_MARKET_DEPTH_TOPIC = "market.$symbol.depth.$type";
	public static final String WEBSOCKET_MARKET_TRADE_TOPIC = "market.$symbol.trade.detail";
	public static final String WEBSOCKET_MARKET_TICKER_TOPIC = "market.$symbol.ticker";

	@Override
	public void subMarketDepth(SubMarketDepthRequest request, ResponseCallback<MarketDepthEvent> callback) {
		// 检查参数
		InputChecker.checker().shouldNotNull(request.getSymbol(), "symbol");

		// 格式化symbol为数组
		List<String> symbolList = SymbolUtils.parseSymbols(request.getSymbol());

		// 检查数组
		InputChecker.checker().checkSymbolList(symbolList);

		String step = request.getStep() == null ? DepthStepEnum.STEP0.getStep() : request.getStep().getStep();
		List<String> commandList = new ArrayList<>(symbolList.size());
		symbolList.forEach(symbol -> {

			String topic = WEBSOCKET_MARKET_DEPTH_TOPIC.replace("$symbol", symbol).replace("$type", step);

			JSONObject command = new JSONObject();
			command.put("sub", topic);
			command.put("id", System.nanoTime());
			commandList.add(command.toJSONString());
		});

		HuobiWebSocketConnection.createMarketConnection(options, commandList, new MarketDepthEventParser(), callback,
				false);

	}

	@Override
	public void subMarketTrade(SubMarketTradeRequest request, ResponseCallback<MarketTradeEvent> callback) {

		// 检查参数
		InputChecker.checker().shouldNotNull(request.getSymbol(), "symbol");

		// 格式化symbol为数组
		List<String> symbolList = SymbolUtils.parseSymbols(request.getSymbol());

		// 检查数组
		InputChecker.checker().checkSymbolList(symbolList);

		List<String> commandList = new ArrayList<>(symbolList.size());
		symbolList.forEach(symbol -> {

			String topic = WEBSOCKET_MARKET_TRADE_TOPIC.replace("$symbol", symbol);

			JSONObject command = new JSONObject();
			command.put("sub", topic);
			command.put("id", System.nanoTime());
			commandList.add(command.toJSONString());
		});

		HuobiWebSocketConnection.createMarketConnection(options, commandList, new MarketTradeEventParser(), callback,
				false);
	}
	
	@Override
	public void subMarketTicker(SubMarketTickerRequest request, ResponseCallback<MarketTickerEvent> callback) {
		// 检查参数
		InputChecker.checker().shouldNotNull(request.getSymbol(), "symbol");

		// 格式化symbol为数组
		List<String> symbolList = SymbolUtils.parseSymbols(request.getSymbol());

		// 检查数组
		InputChecker.checker().checkSymbolList(symbolList);

		List<String> commandList = new ArrayList<>(symbolList.size());
		symbolList.forEach(symbol -> {

			String topic = WEBSOCKET_MARKET_TICKER_TOPIC.replace("$symbol", symbol);

			JSONObject command = new JSONObject();
			command.put("sub", topic);
			command.put("id", System.nanoTime());
			commandList.add(command.toJSONString());
		});

		HuobiWebSocketConnection.createMarketConnection(options, commandList, new MarketTickerEventParser(), callback,
				false);
	}

	public Options getOptions() {
		return options;
	}

	public void setOptions(Options options) {
		this.options = options;
	}

	public HuobiRestConnection getRestConnection() {
		return restConnection;
	}

	public void setRestConnection(HuobiRestConnection restConnection) {
		this.restConnection = restConnection;
	}

}
